<?php
/**
 * @author thanhnv
 * @desc 26Jun2008
 * 
 * @desc convert csv=>database
 * @desc insert province & city from csv file to db
 */
set_time_limit(0);
include_once (PATH_SQL . 'category_sql.php');
$sPath = PATH_CONTROL_ADMIN;
//$d = dir($sPath);

function check_name($name, $db_category)
{
    $sSql = "SELECT * FROM ams_product_category WHERE product_category_name like '{$name}' AND product_category_parent_id=0 ";
    
    return $db_category->getRecordsetByPKExtend($sSql);
}

function get_child($category_id, $name,$db_category)
{
    $sSql = "SELECT * FROM ams_product_category WHERE product_category_parent_id={$category_id} AND product_category_name like '{$name}'";
    return $db_category->getRecordsetByPKExtend($sSql);
}

$db_category = new Db_Categoiry();

$fileCSV =   "../upload/category.csv";

//$fileCSV = $sPath . "category.csv";

echo $fileCSV."<br />";

$handle = fopen($fileCSV, "r");
$sort = 0;
$count = 0;
$pre_id=null;
$aData=array();

while (($data = fgetcsv($handle,1000, ",")) !== false )
{

    $aData=null;
    $pre_id=0;
    if ($sort == 0)
    {
		
    }
    else
    {
    	
        //$arr_temp=mb_convert_encoding($arr_temp,"SJIS","UTF-8");
        $arr = explode('>', $data[1]);
        $count = count($arr);
        for ($i = 1; $i < $count; $i++)
        {        	
            $now_name = mb_convert_encoding($arr[$i],"UTF-8","SJIS");
            $now_name_addslashes = addslashes($now_name);
            //$now_name=$arr[$i];
           	//print_r($now_name);
            //echo "<br/>";
            //THE FIRST ONLY NEED CHECK EXITS OR NOT
            if ($i == 1)
            {
            	$rs_check_name=check_name($now_name_addslashes,$db_category);
                if (!$rs_check_name)
                {
                    $aData['product_category_name'] = $now_name;
                    $aData['product_category_parent_id'] = 0;
                    $aData['product_category_status']=1;
                    $pre_id = $db_category->doInsert($aData, false);
                }
                else
                {
                	$pre_id=$rs_check_name['product_category_id'];
                }
            }
            else
            {
                if ($i < $count - 1)
                {
                    //GET ALL CHILD OF THE PRE CAT
                    $rs_child = get_child($pre_id,$now_name_addslashes,$db_category);
                    if (!is_array($rs_child))
                    {
                        $aData['product_category_name'] = $now_name;
                        $aData['product_category_parent_id'] = $pre_id;
                        $aData['product_category_status']=1;
                        $pre_id = $db_category->doInsert($aData, false);
                    }
                    else
                    {
                    	$pre_id=$rs_child['product_category_id'];
                    }
                }
                //IF THE LAST ITEM INSER WITH YAHOO ID
                else
                {
		    $aData['product_category_yahoo_id'] = $data[0];
                    $aData['product_category_name'] = $now_name;
                    $aData['product_category_parent_id'] = $pre_id;
                    $aData['product_category_status']=1;
                    $pre_id = $db_category->doInsert($aData, false);
                }
            }
        }
    }
    $sort++;
    //echo $sort;
    //echo "<br/>";
}
fclose($handle);
echo "Import sucessful";
//}
//$d->close();


/**
 * @desc finished:
 * province.csv
 * 1.hokaido.csv
 * 2.aomori.csv
 * 3.iwate.csv
 * 4.miyagi.csv
 */
?>